const App = getApp()
import $CountDown from '../../../utils/countdown.js'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    seckillId:String,
    time:{
      type:Number,
      value:'',
      observer(newVal, oldVal, changedPath) {
        // 属性被改变时执行的函数（可选），通常 newVal 就是新设置的数据， oldVal 是旧数
        // 新版本基础库不推荐使用这个字段，而是使用 Component 构造器的 observer 字段代替（这样会有更强的功能和更好的性能）
        // console.log('oldVal:'+oldVal)
        // console.log('newVal:'+newVal)
        if(newVal != oldVal){ 
          this.countDown(newVal)
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    iphoneX: App.globalData.isIphoneX,
    hours:0,
    min: 0,
    sec: 0,
  },

  attached:function(){
    const that = this;
  },
  detached:function(){
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 计算倒计时
    countDown(time){
      if(!time){
        return;
      }
      const that = this;
      const obj = {};
      obj.seckillId  = that.properties.seckillId;
      // console.log('time:'+time)
      let hours = 0
      let min = 0
      let sec = 0
      new $CountDown({
        date: time,
        render(date) {
            hours = this.leadingZeros(date.hours, 2) 
            min = this.leadingZeros(date.min, 2) 
            sec = this.leadingZeros(date.sec, 2)
          that.setData({
            hours:hours,
            min:min,
            sec:sec
          })
          if(hours == 0 && min == 0 && sec == 0){
            var pages = getCurrentPages() //获取加载的页面
            var currentPage = pages[pages.length - 1] //获取当前页面的对象
            currentPage.onLoad(obj)
          }
        }
      })
    },
    myevent(){

    }
  }
})